var isRefreshOddsTable = true;
parent.Game93 = new Array();

function NumFormat(intNum){
	if(intNum < 10)return "00" + intNum.toString();
	if(intNum < 100)return "0" + intNum.toString();
	return intNum.toString();
}

function is3WordNum(intNum){
	if(intNum < 100){
		if(Math.floor(intNum / 10) > (intNum % 10))return false;
	}else{
		if(Math.floor(intNum / 10) > (intNum% 100))return false;
		if(Math.floor((intNum % 100) / 10) > (intNum % 10))return false;
	}
	return true;
}

function clearTextField(){
	var intNum = 0;
	for(intNum = 0; intNum <= 99; intNum++)	{
		if(null != document.ShowOddsForm["Odds"+intNum.toString()]){
			document.ShowOddsForm["Odds"+intNum.toString()].value = "";
		}

	}
}

function GetWagerTable(){
	if(isRefreshOddsTable == false){
		InsertOdds();
		return;
	}

	var objWTable = document.getElementById("wager_table"), i = 0, intNum = 0, strNum = "", strDisabled = "", StopClass = "";
	var ArrGame = eval("parent.Game"+intBetType);
	var ArrStake = new Array();

	with(objWTable)	{
		for(intNum = 0; intNum <= 99; intNum++)	{
			if(null != document.ShowOddsForm["Odds"+intNum.toString()]){
				if(null != document.ShowOddsForm["Odds"+intNum.toString()].value){
					ArrStake[intNum] = document.ShowOddsForm["Odds"+intNum.toString()].value;
				}else{
					ArrStake[intNum] = "";
				}
			}else{
				ArrStake[intNum] = "";
			}

		}
		while(rows.length > 1)	deleteRow(rows.length - 1);
			
		for(intNum = 0; intNum <= 99; intNum++)	{
			if(intBetType == 93 && ArrGame.length <= intNum)continue;

			if(i % 5 == 0){
				var intCellNo = 0;
				newTR = insertRow(rows.length);
			}
			newTR.className = "mcen";
			with(newTR){
				strNum = NumFormat(ArrGame[intNum][3]);

				newTD = insertCell(intCellNo);
				newTD.id = "Num"+intNum.toString();
				newTD.className = "mcen_th";
				newTD.innerHTML = strNum;
				intCellNo++;

				newTD = insertCell(intCellNo);
				newTD.id = "OddsID"+intNum.toString();
				newTD.className = "mbline_ratio";
				newTD.innerHTML = ArrGame[intNum][5];
				intCellNo++;

				newTD = insertCell(intCellNo);
				newTD.id = "OddsValue"+intNum.toString();
				newTD.className = "mbline";
				newTD.innerHTML = "<input name=\"Odds"+intNum.toString()+"\" value=\""+ArrStake[intNum]+"\" type=\"text\" class=\"zamoney\" onkeypress=\"return CheckNumKey()\" onKeyUp=\"return Calculate()\" onchange=\"Calculate()\" "+strDisabled+" size=\"3\">";
				intCellNo++;
			} //with(newTR)
			i++;
		} //for(intNum = 0; intNum <= 99; intNum++)
	} //with(objWTable)
	isRefreshOddsTable = false;
	InsertOdds();
}

function InsertOdds(){
	var ArrGame = eval("parent.Game"+intBetType);
	var newTD1, newTD2, newTD3, intNum = 0;

	for(intNum = 0; intNum <= 99; intNum++)	{
		if(intBetType == 93 && ArrGame.length <= intNum)continue;

		newTD1 = document.getElementById("OddsID"+intNum.toString());
		//newTD2 = document.getElementById("OddsTip"+intNum.toString());
		newTD3 = document.getElementById("OddsValue"+intNum.toString());
		if(ArrGame[intNum][6] == "1"){
			newTD1.className = "mbline_ratio_stop";
			newTD3.className = "mbline_stop";
		}else{
			newTD1.className = "mbline_ratio";
			newTD3.className = "mbline";
		}
		newTD1.innerHTML = ArrGame[intNum][5];
	}
}

function CheckNumKey(){
	if(event.keyCode == 13) return true;
	if((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode > 95 || event.keyCode < 106)){
		alert(notNumber);
		return false;
	}
}

function OtherCheck(strStake, flOdds, intBID, intNum){
	//下注金額必須為數值
	if(isNaN(strStake) == true){
		alert(notNumber);
		return false;
	}
	var intStake = eval(strStake);
	//不能低於最低下注金額
	if(intStake < chkMinStake){
		alert(underMinStake+chkMinStake.toString());
		return false;
	}
	//不能超過本期單注限額
	if(intStake > chkLottoMaxBetLimitValue){
		alert(overLottoMaxBetLimitValue + chkLottoMaxBetLimitValue);
		return false;
	}
	//不能超過單注限額
	if(intStake > chkMaxBetLimitValue){
		alert(overBetLimit + chkMaxBetLimitValue);
		return false;
	}
	//不能超過單場限額
	if(intStake > (chkMaxEventLimitValue - arrChildEveLimitUsed[intNum])){
		alert(overEventLimit + (chkMaxEventLimitValue - arrChildEveLimitUsed[intNum]).toString());
		return false;
	}
	//不能超過最高派彩限額
	if((intStake*eval(flOdds)) > chkMaxPayOff){
		alert(overMaxPayOff+chkMaxPayOff);
		return false;
	}
	return true;
}

function FinalCheck(){
	var objGold, intNum = 0, intTotal = 0, strWager = "";
	var ArrGame = eval("parent.Game"+intBetType);

	for(intNum = 0; intNum <= 99; intNum++){
		if(null == document.ShowOddsForm["Odds"+intNum.toString()])continue;

		objGold = document.ShowOddsForm["Odds" + intNum.toString()];
		if (OtherCheck(objGold.value, ArrGame[intNum][5], intBetType, intNum) == false){
			objGold.focus();
			return false;
		}
		if(objGold.value == "")continue;
		strWager += ArrGame[intNum][3] + "#" + ArrGame[intNum][4] + "#" + objGold.value + "#";
		strWager += intBetType.toString() +"@";
		intTotal += parseInt(objGold.value);
	}
	//請填入下注金額！
	if (intTotal == 0)	{
		alert(noMoney);
		return false;
	}
	//總金額不能超過信用餘額
	if (intTotal > chkCredit) {
		alert(overCredit+chkCredit);
		return false;
	}
	//確定下注？
	if (!confirm(sureStake)) return false;
	document.CommonWagerForm.WagerContent.value = strWager;
	document.CommonWagerForm.submit();
	document.GetOddsForm.submit();
}

function Calculate(){
	var intTotal = 0, objGold;
	for(intNum = 0; intNum <= 99; intNum++){
		if(null == document.ShowOddsForm["Odds"+intNum.toString()])continue;

		objGold = document.ShowOddsForm["Odds" + intNum.toString()];
		if(objGold.value == "")continue;
		intTotal += parseInt(objGold.value);
	}
	//document.getElementById("StakeMoney").innerHTML = intTotal;
}

function CountRefresh(){
	var intGT = 0, intGT2 = 0, strGTm = "", strGTs = "";
	if(intRefreshSec >= RefreshSec)isRefresh = true;
	intGT = parseInt(parent.strGameTime);
	if(intGT < 0)intGT = 0;
	intGT2 = (intGT > 900)?900:intGT;
	strGTm = (Math.floor(intGT2 / 60)).toString();
	if(strGTm.length < 2)strGTm = "0" + strGTm;
	strGTs = (intGT2 % 60).toString();
	if(strGTs.length < 2)strGTs = "0" + strGTs;
	document.getElementById("GameSec").innerHTML = strGTm+":"+strGTs;
	parent.strGameTime = intGT - 1;
	intRefreshSec++;
}

function SubmitOddsForm(){
	if(isRefresh == false){
		document.GetOddsForm.action = "http://tw.i5.yimg.com/i/tw/hp/spirit/w5.gif";
		document.GetOddsForm.submit();
		window.status = "";
		return;
	}
	document.GetOddsForm.action = "number92_odds.php";
	document.GetOddsForm.submit();
	isRefresh = false;
	intRefreshSec = 0;
}

function InitAction(){
	document.GetOddsForm.submit();
	setIntervalID1 = setInterval("SubmitOddsForm();", 30000);
	setIntervalID2 = setInterval("CountRefresh();", 1000);
}

function DisplayWindow(){
	var i = 0;
	score_window.style.display = parent.strDisplay;
	if(parent.strDisplay != "none"){
		for(var i = 0; i < parent.arrImages.length; i++){
			document.getElementById("image" + (i + 1)).src = "../images/ball/"+parent.arrImages[i]+".gif";
		}
	}
}

function getStakeGold(){
	if(document.getElementById("StakeGold").value == "")return "";
	return eval(document.getElementById("StakeGold").value);
}

function getNumArray(){
	return new Array(0, 100);
}

function SelectOENum(intOE){
	if(intOE != 1 && intOE != 0)return;
	var objGold, intNum = 0;
	var arrNum = getNumArray();
	var intGold = getStakeGold();
	
	for(intNum = arrNum[0]; intNum < arrNum[1]; intNum++){
		if(null == document.getElementById("Num" + intNum.toString()))continue;

		var intNumber = document.getElementById("Num" + intNum.toString()).innerHTML*1;
		if (intNumber % 2 == intOE)	{
			objGold = document.ShowOddsForm["Odds" + intNum.toString()];
			objGold.value = intGold;
		}
	}
}

function SelectOUNum(intOU){
	if(intOU != 1 && intOU != 0)return;
	var objGold, intNum = 0;
	var MaxNum = 0, MinNum = 0;
	var arrNum = getNumArray();
	var intGold = getStakeGold();
	var intSpecialOverNumber = 50;

	if(intOU == 1){
		MaxNum = arrNum[1];
		MinNum = intSpecialOverNumber;
	}else{
		MaxNum = intSpecialOverNumber - 1;
		MinNum = 0;
	}
	for(intNum = MinNum; intNum <= MaxNum; intNum++){
		if(null == document.ShowOddsForm["Odds"+intNum.toString()])continue;

		objGold = document.ShowOddsForm["Odds" + intNum.toString()];
		objGold.value = intGold;
	}
}


function SelectNum(intType, intSNum){
	if(intType != 1 && intType != 0)return;
	var objGold, intNum = 0, strNum = "", intLNum = 0, intRNum = 0;
	var arrNum = getNumArray();
	var intGold = getStakeGold();
	
	for(intNum = arrNum[0]; intNum < arrNum[1]; intNum++){
		strNum = intNum.toString();
		if(intNum < 10)strNum = "0" + strNum;
	    intLNum = parseInt(strNum.substr(0,1));
	    intRNum = parseInt(strNum.substr(1,1));

		if ((intType == 0 && intRNum == intSNum) || (intType == 1 && intLNum == intSNum)){
			if(null == document.ShowOddsForm["Odds"+intNum.toString()])continue;

			objGold = document.ShowOddsForm["Odds" + intNum.toString()];
			objGold.value = intGold;
		}
	}
}

function setTrinNo(strTrinNo){
	document.GetOddsForm.TrinNo.value = strTrinNo;
	isRefreshOddsTable = true;
	parent.Game93 = new Array();
	document.GetOddsForm.submit();
}

function setTrinity(strTrinity){
	document.GetOddsForm.Trinity.value = strTrinity;
	isRefreshOddsTable = true;
	parent.Game93 = new Array();
	document.GetOddsForm.submit();
}